//
//  DemoPic.swift
//  swiftDemo
//
//  Created by macMini@yryz on 2022/8/2.
//  Copyright © 2022 lovivid. All rights reserved.
//

import UIKit

func funcResizeImage(inputImage:UIImage?, targetSize:CGSize) -> UIImage? {
    if inputImage == nil {
        return nil;
    }
    if 0 == targetSize.width || 0 == targetSize.height {
        return nil;
    }
    
    if targetSize == inputImage!.size {
        return inputImage;
    }
    
    UIGraphicsBeginImageContextWithOptions(targetSize, false, 1);
    inputImage?.draw(in: CGRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height));
    
    let imageResized = UIGraphicsGetImageFromCurrentImageContext();
        
    UIGraphicsEndImageContext();
    
    return imageResized;
}

class DemoPic: NSObject {
    class func demoLogic() {
        //guard let logoPic = UIImage(named: "logo1024.png") else {
        guard let logoPic = UIImage(named: "logo1024b.png") else {
            print("logo1024.png not found")
            return;
        }
        
        let tempPathURL = NSTemporaryDirectory() //URL.init(fileURLWithPath: NSTemporaryDirectory())
        
        guard let jsonPath = Bundle.main.path(forResource: "logoContents", ofType: "json") else {
            print("logoContents.json not found")
            return;
        }
        do {
            let nsData = try Data(contentsOf: URL(fileURLWithPath: jsonPath))
            //读取Json数据
            let jsonObj = try JSONSerialization.jsonObject(with: nsData)
            guard let jsonDic = jsonObj as? [String:Any] else {
                return;
            }
            guard let images = jsonDic["images"] as? [[String:String]] else {
                return;
            }
            //var images:[[String:String]] = jsonObj["images"];
            //print("images \(images)")
            var loop = -1
            for picInfo in images {
                loop += 1
                guard let sizeStr = picInfo["size"] else {print("\(loop) picInfo no size");continue}
                guard let scaleStr = picInfo["scale"] else {print("\(loop) picInfo no scale");continue}
                //print("sizeStr \(sizeStr) scaleStr \(scaleStr)")
                guard let rangeHead = sizeStr.range(of: "x") else {print("\(loop) picInfo->size no x");continue}
                //if rangeHead != nil {
                let size = sizeStr.prefix(upTo: rangeHead.lowerBound);
                let sizeNum = (size as NSString).floatValue
                guard let rangeHead2 = scaleStr.range(of: "x") else {print("\(loop) picInfo->scale no x");continue}
                //if rangeHead2 != nil {
                let scale = scaleStr.prefix(upTo: rangeHead2.lowerBound);
                let scaleNum = (scale as NSString).floatValue
                let tsize = sizeNum * scaleNum
                // print("sizeNum \(sizeNum) scaleNum \(scaleNum) tsize \(tsize)")
                
                guard let resizePic = funcResizeImage(inputImage:logoPic, targetSize:CGSize(width: CGFloat(tsize), height: CGFloat(tsize))) else {
                    print("\(loop) ResizeImage fail");
                    continue
                }
                let fileName = "logo_\(Int(tsize))_\(Int(scaleNum)).png"
                print("sizeNum \(sizeNum) scaleNum \(scaleNum) tsize \(tsize) filePath \(fileName)")
                
                let filePath = "\(tempPathURL)\(fileName)"
                do {
                    try resizePic.pngData()?.write(to: URL(fileURLWithPath: filePath), options: .atomic)
                    print("image write \(filePath)")
                } catch let errLoop {
                    print("image write \(filePath), err: \(errLoop)")
                    //return false
                }
            }
        } catch let err {
            print("fail to read logoContents.json \(err), \(jsonPath)")
        }
        
        
        
    }
}
